package com.example.redisdemo.util;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hexinglin
 * @created 2021-08-06 15:07
 **/
@Component
public class RedisUtils {
    @Autowired
    private RedisTemplate redisTemplate;

    public <T> void hSetList(String key, String field, List<T> values) {
        redisTemplate.opsForHash().put(key, field, values);
    }
    public <T> List<T> hGetList(String key, String field, Class<T> obj) {
        Object value = redisTemplate.opsForHash().get(key, field);
        if (value != null) {
            return JSONObject.parseArray(value.toString(), obj);
        } else {
            return new ArrayList<>();
        }
    }

}
